﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace pb_ws_client
{
    public partial class PersonalInfo : Form
    {
        private pb_ws_client.WebServiceReference.person_info_r _pi;
        private WebServiceReference.AppServiceSoapClient _wsr = 
            new WebServiceReference.AppServiceSoapClient();
        private main parent = new main();

        public PersonalInfo(WebServiceReference.AppServiceSoapClient wsr, main _form)
        {
            InitializeComponent();
            _wsr = wsr;
            parent = _form;
        }

        public PersonalInfo(WebServiceReference.AppServiceSoapClient wsr, main _form, 
            pb_ws_client.WebServiceReference.person_info_r pi)
        {
            InitializeComponent();
            _wsr = wsr;
            parent = _form;
            _pi = new pb_ws_client.WebServiceReference.person_info_r();
            _pi = pi;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Number_textBox.Text == "" || 
                Name_textBox.Text == "" || 
                Gender_comboBox.Text == "" || 
                IDNumber_textBox.Text == "" || 
                Tel_textBox.Text == "" || 
                Email_textBox.Text == "" || 
                priorityID_comboBox.Text == "")
            {
                MessageBox.Show("您还有未完成的表单", "警告", MessageBoxButtons.OK);
                return;
            }
            if (!IsRegEx("^(-?[0-9]*[.]*[0-9]{0,3})$", Tel_textBox.Text))
            {
                MessageBox.Show("请输入正确的电话号码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!isEmail(Email_textBox.Text))
            {
                MessageBox.Show("请输入邮件地址的正确格式", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            pb_ws_client.WebServiceReference.person_info_r newpi = 
                new pb_ws_client.WebServiceReference.person_info_r();
            newpi.NUMBER = Number_textBox.Text;
            newpi.NAME = Name_textBox.Text;
            newpi.GENDER = Gender_comboBox.Text;
            newpi.NATIONALITY = Nationality_comboBox.Text;
            newpi.BIRTHDAY = dateTimePicker1.Text;
            newpi.IDNUMBER = IDNumber_textBox.Text;
            newpi.TELEPHONE = Tel_textBox.Text;
            newpi.EMAIL = Email_textBox.Text;
            newpi.PRIORITY_ID = priorityID_comboBox.Text;
            if (!newpi.Equals(_pi))
            {
                DialogResult result = new DialogResult();
                result = MessageBox.Show("您确定吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (result == DialogResult.No)
                    return;

                if (_pi.ID == null)
                {
                    newpi.PASSWORD = "222222";

                    if (newpi.PRIORITY_ID == "游客")
                        newpi.PRIORITY_ID = "1";
                    else if (newpi.PRIORITY_ID == "党员/预备党员")
                        newpi.PRIORITY_ID = "2";
                    else if (newpi.PRIORITY_ID == "支部书记")
                        newpi.PRIORITY_ID = "3";
                    else if (newpi.PRIORITY_ID == "总支书")
                        newpi.PRIORITY_ID = "4";
                    

	                try
	                {
		                _wsr.AddPerson(LogicLayer.SID, newpi);
	                }
	                catch (System.Exception ex)
	                {
                        MessageBox.Show("数据库异常！\n请确保登录名没有重复。", "错误", MessageBoxButtons.OK,  MessageBoxIcon.Error);
                        return;
	                }
                    parent.refreshData();
                    this.Close();
                } 
                else
                {
                    newpi.ID = _pi.ID;

                    if (newpi.PRIORITY_ID == "游客")
                        newpi.PRIORITY_ID = "1";
                    else if (newpi.PRIORITY_ID == "党员/预备党员")
                        newpi.PRIORITY_ID = "2";
                    else if (newpi.PRIORITY_ID == "支部书记")
                        newpi.PRIORITY_ID = "3";
                    else if (newpi.PRIORITY_ID == "总支书")
                        newpi.PRIORITY_ID = "4";


                    try
                    {
	                    _wsr.UpdatePerson(LogicLayer.SID, newpi);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("数据库异常！\n请确保登录名没有重复。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    parent.refreshData();
                    this.Close();

                }
            }
            else
                this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PersonalInfo_Load(object sender, EventArgs e)
        {
            if (_pi == null)
            {
                _pi = new pb_ws_client.WebServiceReference.person_info_r();
            } 
            else
            {
                Number_textBox.Text =_pi.NUMBER;
                Name_textBox.Text =_pi.NAME;
                Gender_comboBox.Text = _pi.GENDER;
                Nationality_comboBox.Text = _pi.NATIONALITY;
                dateTimePicker1.Value = Convert.ToDateTime(_pi.BIRTHDAY, null);
                IDNumber_textBox.Text =_pi.IDNUMBER;
                Tel_textBox.Text =_pi.TELEPHONE;
                Email_textBox.Text = _pi.EMAIL;
                priorityID_comboBox.Text = _pi.PRIORITY_ID;
            }
        }

        private static bool IsRegEx(string regExValue, string itemValue)
        {

            try
            {
                Regex regex = new System.Text.RegularExpressions.Regex(regExValue);
                if (regex.IsMatch(itemValue)) return true;

                else return false;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
            }
        }

        public static bool isEmail(string inputEmail)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(inputEmail))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
